package com.wkk.quartz.controller;

import com.wkk.annotation.AutoLog;
import com.wkk.log.common.LogConstant;
import com.wkk.quartz.dto.QuartzAddOrUpdateDto;
import com.wkk.quartz.dto.QuartzShowDto;
import com.wkk.quartz.service.QuartzService;
import com.wkk.utils.JwtUtil;
import com.wkk.verification.group.AddDto;
import com.wkk.verification.group.UpdateDto;
import com.wkk.vo.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author : WangKeKe
 * @date : 2020/8/17 16:46
 * Description : 定时任务控制类
 */

@Slf4j
@RestController
@RequestMapping("/quartz")
@Api(tags = "RESTFUL-管理员-定时任务")
public class QuartzController {

    @Autowired
    private QuartzService quartzService;

    @AutoLog(description = LogConstant.ROLE_NAME_ADMIN + "-获取所有定时任务")
    @ApiOperation("所有定时任务")
    @GetMapping("/list")
    public Result<List<QuartzShowDto>> queryAllScheduleTask() {
        return Result.ok(quartzService.queryList());
    }

    @AutoLog(description = LogConstant.ROLE_NAME_ADMIN + "-启动定时任务")
    @ApiOperation("启动定时任务")
    @ApiImplicitParam(name = "id", value = "定时任务ID", required = true)
    @PutMapping("/start/{id}")
    public Result<String> startScheduledTask(@PathVariable("id") String id) {
        quartzService.resumeJob(id);
        return Result.ok();
    }

    @AutoLog(description = LogConstant.ROLE_NAME_ADMIN + "-立即执行")
    @ApiOperation("立即执行定时任务")
    @ApiImplicitParam(name = "id", value = "定时任务ID", required = true)
    @PutMapping("/execute/{id}")
    public Result<String> executeScheduledTask(@PathVariable("id") String id) {
        try {
            quartzService.execute(quartzService.getById(id));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.ok();
    }

    @AutoLog(description = LogConstant.ROLE_NAME_ADMIN + "-暂停定时任务")
    @ApiOperation("暂停定时任务")
    @ApiImplicitParam(name = "id", value = "定时任务ID", required = true)
    @PutMapping("/stop/{id}")
    public Result<String> stopScheduledTask(@PathVariable("id") String id) {
        quartzService.pause(id);
        return Result.ok();
    }

    @AutoLog(description = LogConstant.ROLE_NAME_ADMIN + "-添加定时任务")
    @ApiOperation("添加定时任务")
    @PostMapping("/add")
    public Result<String> addScheduledTask(@RequestBody @Validated(AddDto.class) QuartzAddOrUpdateDto quartzAddOrUpdateDto,
                                           HttpServletRequest request) {
        return quartzService.addOrUpdateTask(quartzAddOrUpdateDto, JwtUtil.getUserIdFromRequest(request)) ? Result.ok("添加成功") : Result.error("添加失败");
    }

    @AutoLog(description = LogConstant.ROLE_NAME_ADMIN + "-修改定时任务")
    @ApiOperation("修改定时任务")
    @PostMapping("/update")
    public Result<String> updateScheduledTask(@RequestBody @Validated(UpdateDto.class) QuartzAddOrUpdateDto quartzAddOrUpdateDto,
                                              HttpServletRequest request) {
        return quartzService.addOrUpdateTask(quartzAddOrUpdateDto, JwtUtil.getUserIdFromRequest(request)) ? Result.ok("修改成功") : Result.error("修改失败");
    }


}
